import {  Clock, OrthographicCamera, Scene, WebGLRenderer } from 'three'
import * as THREE from 'three'


var timeInAll = 0
var count = 0
class Loop {
  single = null;
  renderer = null;
  cssScene= null;
  scene= null;
  camera= null;
  updatables= [];
  cameraView= [];
  _deviceCtrOpen= null;
  clock= null;

  constructor(
    camera,
    scene,
    renderer,
    single,
  ) {
    if (single) {
      this.single = single
    }

    this.camera = camera
    this.cameraView = []
    // this.camera.layers.enable(1);
    this.scene = scene
    this.renderer = renderer
    this.updatables = []
    this._deviceCtrOpen = false
    // this.stereoEffect = new StereoEffect(this.renderer)
    // this.stereoEffect.setSize(window.innerWidth, window.innerHeight)
    this.clock = new Clock()

  }
  addPanoCameraView(options){
    let view = {
      left: options.left||0,
      top: options.top||0,
      width: options.width||1,
      height: options.height||1,
      background: options.background||new THREE.Color( 0, 0, 0 ),
      opacity: options.opacity||1,
      eye: options.eye||[0, 200, 250],
      up: options.up||[0, 1, 0],
      fov: options.fov||45,
      updateCamera: function (camera, scene) {
          if(options.onUpdateCamera){
            options.onUpdateCamera(camera, scene);
          }
      }
    }
    
    view.camera = options.camera;
    view.renderer = options.renderer;
    this.cameraView.push(view);
     
    return view;
  }
  addCameraView(options){
    let view = {
      left: parseFloat(options.left),
      top: parseFloat(options.top),
      width: parseFloat(options.width),
      height: parseFloat(options.height),
      background: options.background||new THREE.Color( 0, 0, 0 ),
      opacity: parseFloat(options.opacity),
      eye: options.eye||[0, 200, 250],
      up: options.up||[0, 1, 0],
      renderTarget: options.renderTarget||null,
      renderer: options.renderer||null,
      fov: parseFloat(options.fov)||45,
      updateCamera: function (camera, scene,renderer) {
      //					  camera.position.x += mouseX * 0.05;
      //					  camera.position.x = Math.max( Math.min( camera.position.x, 2000 ), -2000 );
          // camera.lookAt(scene.position);//设置相机方向(指向的场景对象)
          if(options.onUpdateCamera){
            options.onUpdateCamera(camera, scene,renderer);
          }
      }
    }
    
    view.camera = options.camera;
    this.cameraView.push(view);
     
    return view;
  }
  animate() {
    // if (this.single) {
    //   requestAnimationFrame(this.animate.bind(this))
    // }
    // window.requestAnimationFrame = 
    requestAnimationFrame(this.animate.bind(this))

    this.tick()



    if (this.camera && this.scene) {
      for (var ii = 0; ii < this.cameraView.length; ++ii) {
        var view = this.cameraView[ii];
        var camera = view.camera;
        if(view.renderer){
          let container = view.renderer.domElement;
          if(view.container){
            container = view.container
          }
          var left = Math.floor(container.clientWidth * view.left);
          var top = Math.floor(container.clientHeight * view.top);
          var width = Math.floor(container.clientWidth * view.width);
          var height = Math.floor(container.clientHeight * view.height);
          camera.aspect = width / height
          view.renderer.setViewport(left, top, width, height);
          view.renderer.setRenderTarget(view.renderTarget);
          view.renderer.render(this.scene, camera);//更新渲染器
          view.updateCamera(camera, this.scene,view.renderer);
        }
        else{
          var left = Math.floor(window.innerWidth * view.left);
          var top = Math.floor(window.innerHeight * view.top);
          var width = Math.floor(window.innerWidth * view.width);
          var height = Math.floor(window.innerHeight * view.height);
          this.renderer.setViewport(left, top, width, height);
          this.renderer.setScissor(left, top, width, height);
          this.renderer.setScissorTest(true);
          this.renderer.setClearColor(view.background,view.opacity);
          this.renderer.setClearAlpha(view.opacity);
          if(camera){
            // camera.aspect = width / height;
            // camera.updateProjectionMatrix();//更新投影矩阵
            this.renderer.setRenderTarget(view.renderTarget);
            this.renderer.render(this.scene, camera);//更新渲染器
            view.updateCamera(camera, this.scene,this.renderer);

          }
          
        }
        
        
      }
    }
    
  }

  start() {
    // if (this.single) {
    //   this.animate()
    // } else {
    //   this.renderer.setAnimationLoop(this.animate.bind(this))
    // }
    this.animate()
  }

  stop() {
    this.renderer.setAnimationLoop(null)
  }

  tick() {
    // only call the getDelta function once per frame!
    const delta = this.clock.getDelta() // console.log(
    //   `The last frame rendered in ${delta * 1000} milliseconds`,
    // );

    if (count < 10000) {
      count++
      timeInAll += delta
    }

    if (count == 10000) {
      console.log('10000 fps cost time in all ' + timeInAll)
      count++
    }

    for (const object of this.updatables) {
      if (object.tick != undefined) object.tick(delta)
    }
  }
}

export { Loop }
